package com.ruoyi.central.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.cache.common.CacheConstants;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.service.ServiceImpl;
import com.ruoyi.central.mapper.TopCentralProductTypeOperationStationMapper;
import com.ruoyi.central.domain.TopCentralProductTypeOperationStation;
import com.ruoyi.central.service.ITopCentralProductTypeOperationStationService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.enums.common.State;
import java.util.Collection;
import java.util.ArrayList;

/**
 * 产品类型与操作工位Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-02
 */
@Service
@Slf4j
@CacheConfig(cacheNames = CacheConstants.EhcacheName.CONTINUAL_CACHE_NAME)
public class TopCentralProductTypeOperationStationServiceImpl extends ServiceImpl<TopCentralProductTypeOperationStationMapper,TopCentralProductTypeOperationStation> implements ITopCentralProductTypeOperationStationService
{
    /**
     * 查询产品类型与操作工位
     * 
     * @param id 产品类型与操作工位主键
     * @param columns 查询指定的列
     * @return 产品类型与操作工位
     */
    @Override
    public TopCentralProductTypeOperationStation selectById(Integer id, SFunction<TopCentralProductTypeOperationStation,?> ... columns){

        if(StringUtils.isNotEmpty(columns)){
            return selectOne(lambdaQuery().select(columns).eq(TopCentralProductTypeOperationStation::getId,id));
        }
        return super.selectById(id);
    }

    /**
     * 查询产品类型与操作工位
         * 注意：只获取状态信息为可用的数据
     * 
     * @param id 产品类型与操作工位主键
     * @param columns 查询指定的列
     * @return 产品类型与操作工位
     */
    @Override
    public TopCentralProductTypeOperationStation selectUsableById(Integer id, SFunction<TopCentralProductTypeOperationStation,?> ... columns)
    {
            LambdaQueryWrapper<TopCentralProductTypeOperationStation> lambdaQueryWrapper = lambdaQuery().eq(TopCentralProductTypeOperationStation::getId,id);
            buildUsableState(lambdaQueryWrapper);
            if(StringUtils.isNotEmpty(columns)){
                lambdaQueryWrapper.select(columns);
            }
            return selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询产品类型与操作工位列表
     *
     * @param topCentralProductTypeOperationStation 产品类型与操作工位
     * @return 产品类型与操作工位
     */
    @Override
    public List<TopCentralProductTypeOperationStation> selectList(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        return getBaseMapper().selectTopCentralProductTypeOperationStationList(topCentralProductTypeOperationStation);
    }

    /**
     * 获取所有的产品类型与操作工位数据
     * 
     * @return Map<Integer,TopCentralProductTypeOperationStation> 集合
     */
    @Cacheable
    @Override
    public Map<Integer,TopCentralProductTypeOperationStation> selectAllMap(){

        return getBaseMapper().selectList(customLambdaQuery())
                .stream().collect(Collectors.toMap(TopCentralProductTypeOperationStation::getId,v->v));
    }

    /**
     * 新增产品类型与操作工位
     *
     * @param topCentralProductTypeOperationStation 产品类型与操作工位
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insert(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        return super.insert(topCentralProductTypeOperationStation);
    }

    /**
     * 新增（批量）产品类型与操作工位
     *
     * @param topCentralProductTypeOperationStationList 实体对象集合
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insertBatch(Collection<TopCentralProductTypeOperationStation> topCentralProductTypeOperationStationList, int batchSize) {
        return super.insertBatch(topCentralProductTypeOperationStationList, batchSize);
    }

    /**
     * 删除 产品类型与操作工位 根据编号
     * @param id
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    /**
     * 批量删除 产品类型与操作工位 根据 编号集合
     * @param ids
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteByIds(Collection<? extends Serializable> ids) {
        return super.deleteByIds(ids);
    }

    /**
     * 批量修改 产品类型与操作工位 根据唯一编号
     * @param topCentralProductTypeOperationStationList
     * @param batchSize  更新批次数量
     * @return true 修改成功 false 修改失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateBatchById(Collection<TopCentralProductTypeOperationStation> topCentralProductTypeOperationStationList, int batchSize) {
        return super.updateBatchById(topCentralProductTypeOperationStationList, batchSize);
    }

    /**
     * 修改 产品类型与操作工位
     * @param  topCentralProductTypeOperationStation
     * @param updateWrapper
     * @return
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean update(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation, LambdaUpdateWrapper<TopCentralProductTypeOperationStation> updateWrapper) {
        return super.update(topCentralProductTypeOperationStation,updateWrapper);
    }

    /**
     * 修改产品类型与操作工位
     *
     * @param topCentralProductTypeOperationStation 产品类型与操作工位
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateById(TopCentralProductTypeOperationStation topCentralProductTypeOperationStation)
    {
        return super.updateById(topCentralProductTypeOperationStation);
    }

    /**
     * 修改产品类型与操作工位状态 根据 id 产品类型与操作工位
     * 
     * @param id 产品类型与操作工位主键
     * @param state 数据状态
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateStateById(Integer id, State state){

        return updateById(buildTopCentralProductTypeOperationStationForUpdateState(id,state));
    }

    /**
     * 修改多个产品类型与操作工位状态 根据 id 产品类型与操作工位
     * 
     * @param ids 产品类型与操作工位主键
     * @param state 数据状态
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateStateByIds(Collection<Integer> ids, State state){

        List<TopCentralProductTypeOperationStation> updateTopCentralProductTypeOperationStationList = new ArrayList(ids.size());

            ids.forEach(id->updateTopCentralProductTypeOperationStationList.add(buildTopCentralProductTypeOperationStationForUpdateState(id,state)));

        return updateBatchById(updateTopCentralProductTypeOperationStationList);
    }

    /**
     * 构建修改TopCentralProductTypeOperationStation 的 修改类
     * @param id 产品类型与操作工位主键
     * @param state 数据状态
     * @return TopCentralProductTypeOperationStation构建的修改模型
     */
    private TopCentralProductTypeOperationStation buildTopCentralProductTypeOperationStationForUpdateState(Integer id, State state){

        TopCentralProductTypeOperationStation topCentralProductTypeOperationStation = new TopCentralProductTypeOperationStation();
        topCentralProductTypeOperationStation.setState(state);
        topCentralProductTypeOperationStation.setId(id);

        return topCentralProductTypeOperationStation;
    }

}
